import {EditType} from "@/type/BaseEnum";
import {message} from "ant-design-vue";
import {ref} from "vue";
import useInstance from "@/hooks/useInstance";
import {addApi, deleteApi, editApi} from "@/api/teacher";

export default function useTeacher(getList:any){
    const { global } = useInstance();
    //新增弹框的ref属性
    const addRef = ref()
    //新增
    const addBtn = () => {
        addRef.value?.show(EditType.ADD)
    }
    //编辑
    const editBtn = (row: any) => {
        addRef.value?.show(EditType.EDIT, row)
    }
    //删除
    const deleteBtn = async (row:any) => {
        let confirm = await global.$myConfirm('确定删除该数据吗?')
        if (confirm) {
            let res = await deleteApi(row.teacherId)
            if (res && res.code == 200) {
                message.success(res.msg)
                getList();
            }
        }
    }
    //新增、编辑保存
    const save = async (item:any) => {
        let res = null;
        if (item.type == EditType.ADD) {
            console.log("新增执行")
            res = await addApi(item)
        } else {
            res = await editApi(item)
        }
        if (res && res.code == 200) {
            message.success(res.msg)
            //刷新表格
            getList();
        }
    }
    return {
        addRef,
        addBtn,
        editBtn,
        deleteBtn,
        save
    }
}
